package com.baidu.lib_home.fragment

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnScrollChangedListener
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.baidu.lib_common.base.BaseFragment
import com.baidu.lib_common.utils.dp2px
import com.baidu.lib_common.utils.getStatusBarHeight
import com.baidu.lib_home.R
import com.baidu.lib_home.adapter.PersonalMusicAdapter
import com.baidu.lib_home.databinding.FragmentHomeBinding
import com.baidu.lib_home.databinding.FragmentPersonalBinding
import com.baidu.lib_leancloud.LeanCloudManager
import com.baidu.lib_leancloud.UserManager
import com.baidu.lib_leancloud.db.FavoriteViewModel
import com.baidu.lib_leancloud.model.Music

class PersonalFragment: BaseFragment<FragmentPersonalBinding>() {
    var isWrite = true
    var isState = true
    var state = "添加状态"
    private val favoriteViewModel: FavoriteViewModel by activityViewModels()
    override fun initUI(savedInstanceState: Bundle?) {
        super.initUI(savedInstanceState)
        mBinding.root.setPadding(0, getStatusBarHeight(requireContext()),0,0)
        var height = 0
        mBinding.userName.post({
            height = mBinding.userName.bottom
        })
        mBinding.scrollView.setOnScrollChangeListener(object : View.OnScrollChangeListener{
            override fun onScrollChange(
                v: View?,
                scrollX: Int,
                scrollY: Int,
                oldScrollX: Int,
                oldScrollY: Int
            ) {
                if(scrollY <= height / 2){
                    mBinding.linearLayout2.setBackgroundColor(Color.argb(scrollY * 255 / (height / 2), 255, 255 ,255))
                    if(!isWrite){
                        mBinding.menu.setBackgroundColor(0)
                        mBinding.state.setTextColor(0)
                        mBinding.more.setBackgroundColor(0)
                        isWrite = true
                    }
                    if(!isState){
                        mBinding.state.text = state
                    }
                }else{
                    if(isWrite){
                        mBinding.menu.setBackgroundColor(Color.BLACK)
                        mBinding.state.setTextColor(Color.BLACK)
                        mBinding.more.setBackgroundColor(Color.BLACK)
                        isWrite = false
                    }
                    if(isState){
                        mBinding.state.text = UserManager.instance.currentUser.username
                    }
                }
                if(scrollY >= height+2){
                    mBinding.scrollView.scrollTo(0,oldScrollY)
                }
            }

        })
        initRecyclerView()
        initEvent()

    }
    fun initRecyclerView(){
        val adapter = PersonalMusicAdapter()
        mBinding.recyclerView.adapter = adapter
        mBinding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        favoriteViewModel.favoriteMusicList.observe(this){musicEntityList ->
            val favoriteMusicIds = musicEntityList.mapTo(mutableSetOf()) { it.musicId }

            // 过滤出所有在收藏ID集合中的音乐
            val favoriteMusics = LeanCloudManager.instance.musics
                .filter { favoriteMusicIds.contains(it.id) }
                .toList()

            adapter.setNewData(favoriteMusics)
        }
    }
    fun initEvent(){
        mBinding.head.setOnClickListener{
            if(UserManager.instance.currentUser.username.equals("user")){
                findNavController().navigate(R.id.action_mainFragment_to_loginFragment)
            }
        }
    }


}